
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  defaultproperty;

import java.util.Hashtable;
import java.util.Map;
import java.util.Vector;

import logger.PoolLogger;
import pool.tablename.Pool;
import utility.ConvertToLower;
import utility.Input;
import database.RecordSearch;
import dbmanager.ChekInMaster;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;
import file.SearchRecord;

public class Property_details implements Defaultproperty
{
	Pool masterobject;
	GetIdName gid;
	Vector<Object> propertylist;
	Vector<Object> propertyvector = new Vector<Object>();
	Vector<Object> propertyvaluevector = new Vector<Object>();
	Hashtable<Object, Object> table =new Hashtable<Object, Object>();
	String VALUE="value";
	String VALUEOBJECT="valueobject"; 
	String TYPE="type";
	PoolLogger pl;
	
	public Property_details() {
		// logger
		pl = (PoolLogger) PoolLogger.getLogger(this.getClass().getName());
	}
	
	
	
	public void getResult(String value) 
	{
		Hashtable<Object, Object> mastermap= masterobject.getHashtabel();
		RecordSearch rs =new RecordSearch();
		Hashtable<Object, Object > condition=new Hashtable<Object, Object>();
		condition.put("mid", value);	
		condition.put("td", "null");
		rs.setConditionMap(condition);
		String result[] =rs.getArrayFromResultSet();
		condition.clear();
		
		for(int k=0;k<propertylist.size();k++)
		{
			Hashtable<Object, Object> property = new Hashtable<Object, Object>();
			Hashtable<Object, Object> propertymap =new Hashtable<Object, Object>();
			property.putAll((Hashtable<Object, Object>)propertylist.get(k));
			propertymap.putAll((Hashtable<Object, Object>)  ConvertToLower.convertHashKey(property));
			String pidvalue = propertymap.get("pid").toString();
			propertyvector.add(pidvalue);
		}
		
		String mid="";
		if(mastermap.containsKey("mid"))
			mid=mastermap.get("mid").toString();
		else if(mastermap.containsKey("name"))
		{				
				mid=mastermap.get("name").toString();
				mid =gid.getId(mid);
		}	
		
		condition.clear();
		condition.put("mid", mid);
		rs.setConditionMap(condition);
		String resultset[] = rs.getArrayFromResultSet();		 
		propertyvaluevector.addAll(propertyvector);
		
		for(int i=0;i<resultset.length;i++)
		{
			String splitresultset[] =resultset[i].split("\t");
			propertyvector.add(splitresultset[Column.pid_index-1]);
			String pv = splitresultset[Column.pv_index-1];
			if(pv.equals("0"))
				pv = splitresultset[Column.vt_index-1];
			propertyvaluevector.add(pv);
		}
		
		for(int i=0;i<result.length;i++)
		{
			String splitresult[]=  result[i].split("\t");
			condition.put("mid", splitresult[Column.pv_index-1]);
			rs.setConditionMap(condition);
			String line[]=rs.getArrayFromResultSet();
			boolean flag=false;
			String defaultvalue="error";
			String property = splitresult[Column.pid_index-1];
			if(splitresult[Column.pv_index-1].equals("0")|| line.length==0)
				if(!propertyvector.contains(property))
				{
					Hashtable<Object, Object> propertymap = new Hashtable<Object, Object>();
					propertymap.put("pid", property);
					String pv = splitresult[Column.pv_index-1];
					if(pv.equals("0"))
					{
						pv = splitresult[Column.vt_index-1];
						propertymap.put("pv", "0");
						propertymap.put("vt", pv);
					}
					else
					{
						propertymap.put("pv", pv);
						propertymap.put("vt", "no$");
					}
					propertylist.add(propertymap);
				}
			for(int j=0;j<line.length;j++)
			{
				String splitline[] = line[j].split("\t");
				if(!propertyvector.contains(property))
				{
					int count =checkCondition(splitline[Column.mid_index-1]);
					
					if(count==1)
					{		
					    GetDefaultvalue getDefaultvalue = new GetDefaultvalue(masterobject);
						defaultvalue=getDefaultvalue.getdefaultvalue(splitline[Column.pid_index-1], splitline[Column.pv_index-1]);
						if(!defaultvalue.equalsIgnoreCase("error"))
						{
							Hashtable<Object, Object> propertymap = new Hashtable<Object, Object>();
							propertymap.put("pid", property);
							ChekInMaster checkmaster =new ChekInMaster();
							if(checkmaster.isInMaster(defaultvalue))
							{
								propertymap.put("pv", gid.getId(defaultvalue));
								propertymap.put("vt", "no$");
							}	
							else
							{	
								propertymap.put("pv", "0");
								propertymap.put("vt", defaultvalue);
							}	
							propertylist.add(propertymap);
							flag = true;
						}	
					}
					else if(count==2)
					{	
						Hashtable<Object, Object> propertymap = new Hashtable<Object, Object>();
						propertymap.put("pid", property);
						propertymap.put("pv", splitline[Column.mid_index-1]);
						propertymap.put("vt", "no$");
						propertylist.add(propertymap);
						flag = true;
					}	
					
				}
				if(splitline[Column.pid_index-1].equalsIgnoreCase(VALUE))
				{	
					 Hashtable<Object, Object> propertymap = new Hashtable<Object, Object>();
					 propertymap.put("pid", property);
					 propertymap.put("pv", splitline[Column.pv_index-1]);
					 propertymap.put("vt", "no$");
					 propertylist.add(propertymap);
					 flag = true;
				}
					
			}

		}
		masterobject.setPropertyVector(propertylist);
	}
	
	public void setObject(Object object) 
	{	
		masterobject = (Pool) object;
		gid =new GetIdName();
		VALUE = gid.getId(VALUE);
		propertylist = masterobject.getpropertyVector();
	}
	
	
	public int checkCondition(String conditionobject)
	{	
		try 
		{
			//String MASTERPATH = Input.MASTERREPORTSPATH +conditionobject +"//" +conditionobject + "_mid" + Input.FILRFORMAT;
			table.clear();
			RecordSearch rs =new RecordSearch();
			table.put("mid", conditionobject);
			table.put("td", "null");
			rs.setConditionMap(table);
			String line[] = rs.getArrayFromResultSet();
			table.clear();
			for(int i=0;i<line.length;i++)
			{
				String splitline[]=line[i].split("\t");
				String value = splitline[Column.pv_index-1];
				if(value.equals("0"))
					value = splitline[Column.vt_index-1];
				else
					value = gid.getItem(value);
				table.put(gid.getItem(splitline[Column.pid_index-1]), value);
			}
			
			String conditionvalue;
			String conditionfield;
			try 
			{
				table = ConvertToLower.convertHashKey(table);
				conditionvalue = table.get("conditionvalue").toString();
				conditionfield = table.get("conditionfield").toString();
			} catch (NullPointerException e) 
			{
				table = ConvertToLower.convertHashKey(table);
	            if(table.containsKey("class"))
	                            return 1;
	            else
	            		return 2;
			}		
			for(int i=0;i<propertylist.size();i++)
			{
				Hashtable<Object, Object> property =new Hashtable<Object, Object>();
				property.putAll((Map<? extends Object, ? extends Object>) propertylist.get(i));
				if(property.get("pid").equals(conditionfield))
				{
					if(property.containsKey("pv"))
					{
						if(property.get("pv").equals(conditionvalue))
							return 1;
						else
							return 0;
					}
					if(property.containsKey("vt"))
					{
						if(property.get("vt").equals(conditionvalue))
							return 1;
						else
							return 0;
					}
				}	
			}
			if(propertyvector.contains(conditionfield))
			{	
				String value = propertyvaluevector.get(propertyvector.indexOf(conditionfield)).toString();
				if(value.equals(conditionvalue))
					return 1;
			}
		} catch (Exception e) 
		{
			e.printStackTrace();
			
		}
		return 3;
	}
	
	
	public Hashtable<Object,Object>  getAllproperties() 
	{
		Hashtable<Object,Object> propertymap =new Hashtable<Object,Object>();
		Hashtable<Object, Object> mastermap= masterobject.getHashtabel();
		//for()
		return propertymap;
	}

		
}
